<?php

namespace app\merchant\controller;

use app\common\lib\IAuth;
use think\Cache;
use think\Controller;
use think\Request;
use think\Session;

class Config extends Controller
{
    /**
     * 商户基础设置
     *
     * @return \think\Response
     */
    public function merchant()
    {
        //获取redis中的商户id
        $mer_id = Cache::store('redis')->get(config('redis.merchant_id').'_'.Session::get(config('user.user_id')));
        if(\request()->isPost()){
            //获取修改的商户基本设置参数
            $data = input('post.');
            $validate = validate('merchant');
            if(!$validate->scene('add')->check($data)){
                $this->result($_SERVER['HTTP_REFERER'],config('code.error'),$validate->getError());
            }else{
                //密码MD5加密
                if(!empty($data['pwd'])){
                    $data['pwd'] = IAuth::setPassword($data['pwd']);
                }else{
                    unset($data['pwd']);
                }
                if(empty($data['logo'])){
                    unset($data['logo']);
                }else if(empty($data['txt_info'])){
                    unset($data['txt_info']);
                }
                //保存商家设置
                $id = model('merchant')->allowField(true)->save($data,['mer_id'=>$mer_id]);
                if($id){
                    $this->result($_SERVER['HTTP_REFERER'],config('code.success'),'添加成功');
                }else{
                    $this->result($_SERVER['HTTP_REFERER'],config('code.error'),'添加失败');
                }
            }
        }else{
            //获取用户信息
            $merchant = model('merchant')->get(['mer_id'=>$mer_id]);
            return $this->fetch('',['merchant'=>$merchant]);
        }
    }

    /**
     * 店铺管理.
     *
     * @return \think\Response
     */
    public function store()
    {
        return $this->fetch();
    }

    /**
     * 添加店铺
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function store_add()
    {
        return $this->fetch();
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function map()
    {
        return $this->fetch();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
